package com.qu.waibao.my.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.google.gson.reflect.TypeToken;
import com.qu.waibao.AppConfig;
import com.qu.waibao.R;
import com.qu.waibao.base.BaseFragment;
import com.qu.waibao.base.BaseRecyclerAdapter;
import com.qu.waibao.bean.Coupon;
import com.qu.waibao.interf.XnetCallBack;
import com.qu.waibao.my.adapter.CouponAdapter;
import com.qu.waibao.network.ActionUtil;
import com.qu.waibao.network.NetManager;
import com.qu.waibao.utils.GsonUtil;
import com.qu.waibao.utils.ToastUtil;
import com.qu.waibao.widget.RecyclerRefreshLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * @描述 describe
 * @作者 Administrator
 * @创建时间 2018/7/25 17:35
 * @邮箱 1123599561@qq.com
 */
public class CouponFragment extends BaseFragment implements RecyclerRefreshLayout.SuperRefreshLayoutListener,
        BaseRecyclerAdapter.OnItemClickListener
{
    @BindView(R.id.mRecyclerRefreshLayout)
    RecyclerRefreshLayout mRecyclerRefreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private CouponAdapter adapter;
    private int pageNum = 1;
    private boolean isRefreshing = true;
    private String type = "";

    @Override
    protected void initBundle(Bundle bundle) {
        super.initBundle(bundle);
        if(bundle != null){
            type = bundle.getString("type");
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_coupon;
    }

    @Override
    protected void initWidget(View root) {
        super.initWidget(root);
        mRecyclerRefreshLayout.setColorSchemeResources(R.color.colorPrimaryDark);
        mRecyclerRefreshLayout.setSuperRefreshLayoutListener(this);
    }

    @Override
    protected void initData() {
        super.initData();
        adapter = new CouponAdapter(mContext, BaseRecyclerAdapter.ONLY_FOOTER);
        LinearLayoutManager layoutManager= new LinearLayoutManager(mContext);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setAdapter(adapter);
        if(adapter.getItems().size() == 0){
            mRecyclerRefreshLayout.setRefreshing(true);
            onRefreshing();
        }
        adapter.setOnItemClickListener(this);
    }
    public void requestData(){
        Map<String,String> map = new HashMap<>();
        map.put("action", ActionUtil.myquan_list_get);
        map.put("userid", AppConfig.getString("userid"));
        map.put("state",type);
        map.put("pagenum",pageNum+"");
        NetManager.getInstance().request(getActivity(), map, false, new XnetCallBack() {
            @Override
            public void onSuccess(String result, String msg) {
               List<Coupon> list = GsonUtil.createGson().fromJson(result,
                       new TypeToken<List<Coupon>>(){}.getType());
               if(isRefreshing){
                   adapter.clear();
                   adapter.addAll(list);
                   if(list.size() < 12){
                      adapter.setState(BaseRecyclerAdapter.STATE_NO_MORE,true);
                   }
               }else {
                   adapter.addAll(list);
                   if(list.size() < 12){
                       adapter.setState(BaseRecyclerAdapter.STATE_NO_MORE,true);
                   }
                   if(list.size() == 0){
                       pageNum--;
                   }
               }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                if(pageNum > 1){
                    pageNum--;
                    onComplate();
                }
            }

            @Override
            public void onFinished() {
                onComplate();
            }
        });
    }

    @Override
    public void onRefreshing() {
        pageNum = 1;
        isRefreshing = true;
        requestData();
    }

    @Override
    public void onLoadMore() {
        pageNum++;
        isRefreshing = false;
        adapter.setState(BaseRecyclerAdapter.STATE_LOAD_MORE,true);
        requestData();
    }

    public void onComplate(){
        mRecyclerRefreshLayout.onComplete();
        isRefreshing = false;
    }

    @Override
    public void onItemClick(int position, long itemId) {

    }
}
